// pages/my/my.ts

import { getUserInfo } from '../../../http/api/userServiceApi'
import { getBalance, withdraw } from '../../../http/api/driverService'
import { getVehicleSystem, obtainVehicleType } from '../../../http/api/positionApi'
import Message from 'tdesign-miniprogram/message/index'

const tokenMixin = require('../../../mixin/tokenMixin')

// @ts-ignore
const app = getApp()
Page({
    behaviors: [tokenMixin],
    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        url: '',
        username: '未登录',
        userInfo: {},
        carInfo: {},
        money: 0
    },

    call(e: any) {
        console.log(e.currentTarget.dataset.phone)
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
        })
    },

    toDriverJoining() {
        wx.navigateTo({
            url: '/pages/driver/driverJoining/driverJoining?userInfo=' + JSON.stringify(this.data.userInfo)
        })
    },
    DelLogin() {  
        wx.removeStorageSync('openid')
        wx.removeStorageSync('token')
        wx.setStorageSync('user-type', 2)
        wx.redirectTo({
            url: '/pages/my/login/login'
        })
    },
    toUpdateUser() {
        // @ts-ignore
        app.globalData.userInfo = this.data.userInfo
        wx.navigateTo({
            url: `/pages/user/userUpdate/userUpdate`
        })
    },

    login() {
        wx.navigateTo({
            url: '/pages/my/login/login'
        })
    },

    toMyWallet() {
        wx.navigateTo({
            url: '/pages/driver/wallet/wallet'
        })
    },

    async toWithdraw() {
        const { money } = this.data
        await withdraw(money)
        await this.getWallet()
    },

    async getWallet() {
        const res: any = await getBalance()
        this.setData({
            money: res.data.data[0].user_money
        })
        const config: any = await getVehicleSystem()

        if (Number(config[0].not_use_money.toFixed(2)) > res.data.data[0].user_money) {
            console.log(Number(config[0].not_use_money.toFixed(2)), res.data.data[0].user_money, '-----快充钱')
            Message.warning({
                context: this,
                offset: [20, 32],
                duration: 5000,
                icon: false,
                content: `司机保证金低于¥${Number(config[0].not_use_money.toFixed(2))},请前往钱包充值`
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */ async onLoad() {},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    async onShow() {
        await this.getWallet()
        const token = wx.getStorageSync('token')
        if (token) {
            this.setData({
                isLogin: true
            })

            const res: any = await getUserInfo()
            const data: any = await obtainVehicleType()
            const userInfo = res.data.data[0]
            console.log(userInfo, '---用户信息')
            const carInfo = data.find((item: any) => item.ID == userInfo.car_id)
            this.setData({
                username: userInfo.user_sname,
                url: userInfo.user_icon,
                userInfo,
                carInfo
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {}
})
